package com.zone.vehicle.superapp.view.appmanager;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;

import com.tamsiree.rxui.view.tablayout.listener.OnTabSelectListener;
import com.zone.lib.download.enums.AppTypeEnum;
import com.zone.vehicle.superapp.bean.AppBean;
import com.zone.vehicle.superapp.databinding.FragmentAppBinding;
import com.zone.vehicle.superapp.databinding.FragmentAppTabItemBinding;
import com.zone.vehicle.superapp.model.AppManagerViewModel;
import com.zone.vehicle.superapp.model.ViewModelFactory;
import com.zone.vehicle.superapp.view.appmanager.tab.AppListFragment;
import com.zone.vehicle.superapp.view.appmanager.tab.AppUpdateListFragment;
import com.zone.vehicle.superapp.view.appmanager.tab.LightAppListFragment;
import com.zone.vehicle.superapp.view.common.base.BaseFragment;

import org.apache.commons.lang3.ObjectUtils;

import java.util.List;

public class AppManagerFragment extends BaseFragment {


    private FragmentAppBinding binding;

    private AppManagerFragmentPagerAdapter adapter;

    private static AppManagerViewModel viewModel;

    private static final String[] tabs = {"下载", "轻应用", "更新"};

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = FragmentAppBinding.inflate(inflater);
        viewModel = new ViewModelProvider(requireActivity(), new ViewModelFactory()).get(AppManagerViewModel.class);
        initAdapter();
        initView();
        return binding.getRoot();
    }

    private void initView(){
        binding.viewPager.setAdapter(adapter);
        binding.tabLayout.setTabData(tabs);
        viewModel.getActivityApp().observe(getViewLifecycleOwner(), new Observer<List<AppBean>>() {
            @Override
            public void onChanged(List<AppBean> appBeans) {
                if (ObjectUtils.isNotEmpty(appBeans)) {
                    tabs[0] = "下载 (" + appBeans.size() + ")";
                }else {
                    tabs[0] = "下载 (0)";
                }
                binding.tabLayout.setTabData(tabs);
                adapter.notifyDataSetChanged();
            }
        });
        viewModel.getNeedUpdateApps().observe(getViewLifecycleOwner(), new Observer<List<AppBean>>() {
            @Override
            public void onChanged(List<AppBean> appBeans) {
                if (ObjectUtils.isNotEmpty(appBeans)) {
                    tabs[2] = "更新 (" + appBeans.size() + ")";
                }else {
                    tabs[2] = "更新 (0)";
                }
                binding.tabLayout.setTabData(tabs);
                adapter.notifyDataSetChanged();
            }
        });
        binding.tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                binding.viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                binding.tabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        viewModel.getDownloadApps(getActivity(), AppTypeEnum.APP);
        viewModel.getNeedUpdateApps(getContext());
    }

    @Override
    public void initFragmentPages() {
        addPage(new AppListFragment());
        addPage(new LightAppListFragment());
        addPage(new AppUpdateListFragment());
    }

    private void initAdapter(){
        adapter = new AppManagerFragmentPagerAdapter(getContext(), getChildFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }



    private class AppManagerFragmentPagerAdapter extends FragmentPagerAdapter {

        private FragmentAppTabItemBinding binding;
        private Context context;
        public AppManagerFragmentPagerAdapter(Context context, @NonNull FragmentManager fm, int behavior) {
            super(fm, behavior);
            this.context = context;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return getPage(position);
        }

        @Nullable
        @org.jetbrains.annotations.Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return tabs[position];
        }

        @Override
        public int getCount() {
            return getPages().size();
        }

//        public View getTabView(int position, ViewGroup viewGroup){
////            binding = FragmentAppTabItemBinding.inflate(LayoutInflater.from(context));
////            String[] tab_title = context.getResources().getStringArray(R.array.tab_title);
////            binding.title.setText(tab_title[position]);
//            return binding.getRoot();
//        }
    }

}